<?php
/******************************************************************************
 Filter Customization
 created by Sivakumaran
 File Description:
 		To create new customizations and edit existing customizations..
	 	
/*****************************************************************************/
include(DOCINCLUDES."global_files/global_locale.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/report_php.inc");
include(DOCINCLUDES."global_files/tables.inc");
include(DOCINCLUDES."global_files/global_session.inc");
include(DOCINCLUDES."global_files/global_form.inc");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_validations.inc");
$db=new cDatabase();
$page=new cPage();
$form=new cForm();
$dv=new cDataValidation;
if($_GET)
{
$customization_id =$_GET["customization_id"];
$action=$_GET["action"];
$report_id=$_GET["report_id"];
$page->header_path="../../";
if($report_id)
	{
	$db->query="select customization_id from $sys_report_customization_table where customization_name ilike 'Default' and report_id=$report_id;";
	$db->runQuery();
	$db_cus_id=$db->returnArrays();
	$customization_id=$_GET["customization_id"]?$_GET["customization_id"]:$db_cus_id[0]["customization_id"];
	}
$partner_id =$_GET["partner_id"];
}
if($_POST)
	{
	$form->validations["customization_name"]=array("isEmpty"=>"Enter Customization Name");
	$form->validate($_POST);
	if(!$form->errors)
	{
	$customization_name=$_POST["customization_name"];
	$selected_filter_id=explode(",",$_POST["selected_filters"]);

	$selected_column_id=explode(",",$_POST["selected_columns"]);
	$action=$_POST["action"];

	$report_id=$_POST["report_id"];
	if($selected_filter_id && $selected_column_id)
		{
		$c=count($selected_filter_id);
		$column_count=count($selected_column_id);
		$db->query="begin";
		$db->runQuery();

		if($action=="edit")
		{
		$customization_id=$_POST["customization_id"];
		
		$db->query="select is_active from $sys_report_customization_table where customization_id=$customization_id and is_active='t'";
		$db->runQuery();
		$activate_status_array=$db->returnArrays();
		
		if(!$_POST["activate"] && $activate_status_array)
			{
			$is_active_field=" ,is_active='f' ";
			}
		else
			$is_active_field="";	
		
		$db->query="delete
				from
				$sys_filter_customization_table
				where
				customization_id=$customization_id";
		$db->runQuery();

		$db->query="update
				$sys_report_customization_table
				set
				customization_name='$customization_name'
				$is_active_field
				where customization_id=$customization_id";
		$db->runQuery();

		//to delete data for the selected customziation id from _xxxxxxreport_customziation_table in order to store newly selected columns

		$db->query="delete
				from
				$report_customization_table
				where
				customization_id=$customization_id";
		$db->runQuery();
		}
		if($action=="insert")
			{
			$db->query="select
				
nextval('$company_id.sys_report_cus_id_seq')
					as customization_id;";
			$db->runQuery();
			$db_custom_id=$db->returnArrays();
			$customization_id=$db_custom_id[0]["customization_id"];
			$db->query="insert into
					$sys_report_customization_table
					(customization_id,report_id,customization_name)
					values
					($customization_id,$report_id,'".$customization_name."');";
			$db->runQuery();
			}
		for($i=0;$i<$c;$i++)
			{
			$filter_id=explode("-",$selected_filter_id[$i]);
			$db->query="select
					filter_name,
					form_display
					from
					$sys_master_filter_table

					where
					filter_id in ($filter_id[0])";
			$db->runQuery();
			$filter_name_array=$db->returnArrays();

			$filter_name="filter_".$filter_name_array[0]["filter_name"];
			$filter_display_name=$filter_name_array[0]["form_display"];
			if($_POST[$filter_name]!="")
				{

				$default_field=",default_value";
				$default_field_value=",'$_POST[$filter_name]'";
				}
// d't delete*******			else
// 				{
// 				foreach($default_array as $key=>$value)
// 				{
// 					if($value["filter_display_name"]==$filter_display_name)
// 					{
// 					$default_field=",default_value";
// 					$default_field_value=",'$value[default_value]'";
// 					}
// 				}
// 	********			}
			$db->query="insert into
					$sys_filter_customization_table
					(customization_id,
					filter_id,
					filter_display_name,
					filter_order
					$default_field
					)

					values
					($customization_id,
					$filter_id[0],
					'$filter_display_name',
					$i+1
					$default_field_value)";
			$db->runQuery();
			unset($default_field_value,$default_field);
			}
		for($i=0;$i<$column_count;$i++)
			{
			$db->query="select
					mcp.script_variable_name
					from
					$master_column_mapping_table mcp where
					column_id =$selected_column_id[$i]";
			$db->runQuery();
			$db_column_name_array=$db->returnArrays();

			$db_column_name="column_".$db_column_name_array[0]["script_variable_name"];

			$report_details=explode('}',$_POST[$db_column_name]);

			$field="";
			$value="";
			if($report_details[2])
				{
				$field.="style,";
				$value.="'".$report_details[2]."',";
				}
			if($report_details[3])
				{
				$field.="format_details,";
				$value.="'".$report_details[3]."',";
				}
			if($report_details[4])
				{
				$field.="decimal_places,";
				$value.=$report_details[4].",";
				}
			if($report_details[5])
				{
				$field.="is_group,";
				$value.="'".$report_details[5]."',";
				}
			if($report_details[6])
				{
				$field.="display_summary,";
				$value.="'".$report_details[6]."',";
				}

			$db->query="insert into
					$report_customization_table
					(column_id,
					display_name,
					$field
					display_order,
					customization_id)

					values
					($selected_column_id[$i],
					'$report_details[1]',
					$value
					$i+1,
					$customization_id)";
			$db->runQuery();

			}
		if($_POST["activate"])
			{
			$db->query="update $sys_report_customization_table set is_active='f' where report_id=$report_id";
			$db->runQuery();
			$db->query="update $sys_report_customization_table set is_active='t' where customization_id=$customization_id";
			$db->runQuery();
			}
		else
			{
			$db->query="select is_active from $sys_report_customization_table where report_id=$report_id and is_active='t'";
			$db->runQuery();
			$activate_status_array=$db->returnArrays();
			
			if(!$activate_status_array)
				{
				$db->query="update $sys_report_customization_table set is_active='t' where report_id=$report_id and customization_name ilike 'default'";
				$db->runQuery();
				
				}
			}
		if ($db->result)
			{
			$db->query="commit";
			$db->runQuery();
			unset($selected_filter_id);
			unset($selected_column_id);
		
header("Location:../../lrp/reports/report_page.php?customization_id=$customization_id&report_id=$report_id");
			exit;
			}
		}
	}
}
///************************************************************************************//
$page->doHeaderHtml();
$page->doCentreHtml();

	$db->query="select report_name from $master_report_table where report_id=$report_id";
	$db->runQuery();
	$report_name_array=$db->returnArrays();
	$report_name=$report_name_array[0]["report_name"];

$page->title=$page->heading="Customizing $report_name";


/// fetch all filter details for this report id
	$db->query="select
			smf.filter_id,
			smf.filter_name,
			smf.form_display,
			smf.form_field_name[1] as form_field_name1,
			smf.filter_table_join,
			smf.fetch_condition,
			smf.order_by

			from $sys_master_filter_table smf
			join $sys_report_filter_table srf
			ON smf.filter_id=srf.filter_id

			where srf.report_id=$report_id";
	$db->runQuery();
	$master_filter_details_array=$db->returnArrays();

// fetch all filter details for this customization id

	$db->query="select
			filter_id,
			filter_display_name,
			default_value

			from
			$sys_filter_customization_table

			where
			customization_id=$customization_id ";
	$db->runQuery();

	$customized_filter_details_array=$db->returnArrays();

//fetch filter_id and filter display name for this customization id
	$db->query="select
			smf.filter_id||'-'||smf.filter_name as filter_id,
			smf.form_display

			from
			$sys_master_filter_table smf JOIN
			$sys_filter_customization_table sfc
			on smf.filter_id=sfc.filter_id

			where sfc.customization_id=$customization_id
			order by sfc.filter_order;";
	$db->runQuery();
	$customized_filter=$db->returnArrays();

//fetch filter id which are not selected for this customization
	$db->query="select
			smf.filter_id||'-'||smf.filter_name as filter_id,
			smf.form_display

			from $sys_master_filter_table smf
			JOIN $sys_report_filter_table srf
			ON smf.filter_id=srf.filter_id

			where srf.report_id =$report_id and smf.filter_id not in (select filter_id from $sys_filter_customization_table where customization_id=$customization_id)";
	$db->runQuery();
	$non_customized_filters=$db->returnArrays();


/// fetch full details of all columns from database for this report id

	$db->query="select
			mcm.column_id,
			mrc.display_name,
			mrc.style,
			mrc.format_details,
			mrc.decimal_places,
			mrc.display_summary,
			mcm.script_variable_name

			from $master_column_mapping_table mcm
			join $master_report_customization_table mrc
			on mcm.column_id = mrc.column_id
			where mcm.report_id=$report_id and mcm.fetch_column='t'";
	$db->runQuery();
	$master_column_details_array=$db->returnArrays();

///fetch all column details for this customization_id///

	$db->query="select
			rc.column_id,
			rc.display_name,
			rc.style,
			rc.format_details,
			rc.decimal_places,
			rc.is_group,
			rc.display_summary

			from $report_customization_table rc
			where rc.customization_id = $customization_id";
	$db->runQuery();

	$customized_column_details_array=$db->returnArrays();

//to fetch report columns which are already selected for this customization////

	$db->query="select
			rc.column_id,
			rc.display_name

			from
			$report_customization_table rc
			join $master_column_mapping_table mcm on
mcm.column_id=rc.column_id
			where customization_id = $customization_id
			and mcm.fetch_column='t' order by display_order";
	$db->runQuery();
	$customized_columns_array=$db->returnArrays();


//to fetch report column names which are not been selected for displaying in a particular report

	$db->query="select
			mcm.column_id,
			mrc.display_name

			from $master_column_mapping_table mcm
			join $master_report_customization_table mrc
			on mcm.column_id = mrc.column_id

			where mcm.column_id not in (select column_id from $report_customization_table rc where rc.customization_id = $customization_id) and mcm.report_id=$report_id and mcm.fetch_column='t'";
	$db->runQuery();
	$non_customized_columns_array=$db->returnArrays();

	if($action=="edit")
		{
		$db->query="select
				customization_name
				from
				$sys_report_customization_table
				where
				customization_id=$customization_id";
		$db->runQuery();
		$custom_name_array=$db->returnArrays();

		$_POST["customization_name"]=$_POST["customization_name"]?$_POST["customization_name"]:$custom_name_array[0]["customization_name"];
		}

	echo "<form name =\"customization_form\" action =\"$PHP_SELF\" method=\"post\" onsubmit=\"posted_values(customization_form.sDisabled);posted_values1(customization_form.selectedcolumns);assignColumnValues(last_id);\">";

// this for each loop is used to generate hidden elements with name as script variable name and value as imploded(}) custom clumn details....
	foreach($master_column_details_array as $key=>$value)
		{
		$custom_column_details=$value["column_id"]."}".$value["display_name"]."}".$value["style"]."}".$value["format_details"]."}".$value["decimal_places"]."}".$value["is_group"]."}".$value["display_summary"]."}";
		foreach($customized_column_details_array as $key1=>$value1)
			{
			if($value["column_id"]==$value1["column_id"])
				{
				$custom_column_details=implode('}',$value1);
				}
			}
		$form->properties=array("type"=>"hidden","name"=>"column_".$master_column_details_array[$key][script_variable_name],"value"=>$custom_column_details);
		echo $form->generateInputTag();

		unset($custom_column_details);
		}

	$c=count($master_filter_details_array);
	for($i=0;$i<$c;$i++)
		{
		if($master_filter_details_array[$i]["fetch_condition"])
			$condition= "where ". $master_filter_details_array[$i]["fetch_condition"]."";
		if($master_filter_details_array[$i]["order_by"])
			$order_by="order by ".$master_filter_details_array[$i]["order_by"]."";
		$db->query="select
				".$master_filter_details_array[$i]["form_field_name1"]."

				from
				".$master_filter_details_array[$i][filter_table_join]." ".$condition." ".$order_by .";";
		$db->runQuery();
		$filter_array[$master_filter_details_array[$i]["filter_id"]]=$db->returnArrays();

		/// this for loop is to assign default filter value to hidden elements with name as form display and value as default value imploded by } separator....

		foreach($customized_filter_details_array as $key=>$value)
			{
			if(($master_filter_details_array[$i]["filter_id"])==$value["filter_id"])
				{
				$custom_filter_value=$value["default_value"];
// 				echo $custom_filter_value;
				}
			}
		$form->properties=array("type"=>"hidden","name"=>"filter_".$master_filter_details_array[$i][filter_name],"value"=>$custom_filter_value);
		echo $form->generateInputTag();
		unset($custom_filter_value);
		unset($condition,$order_by);
		}
// get the member id from the session
$partner_id=($_REQUEST["partner_id"])?$_REQUEST["partner_id"]:$_SESSION["gmember_id"];
$company_id=($_REQUEST["company_id"])?$_REQUEST["company_id"]:$_SESSION["gmember_id"];

$form->properties=array("type"=>"hidden","name"=>"action","value"=>$action);
		echo $form->generateInputTag();
$form->properties=array("type"=>"hidden","name"=>"report_id","value"=>$report_id);
		echo $form->generateInputTag();
$_POST["sEnabled"][0]=$_POST["sEnabled"]?$_POST["sEnabled"]:$non_customized_filters[0]["filter_id"];
		//for selecting filters
$filter_elements[0]['Available Filters']=
	$form->generateSelectMenu("sEnabled",$_POST["sEnabled"],$non_customized_filters,"","10","","ondblClick=\"MoveOption(customization_form.sEnabled,customization_form.sDisabled);\"title='Double Click to select'","xmedium");

$form->properties=array("type"=>"button","name"=>"Disable","accesskey"=>"3","title"=>"click me to select Filter ","class"=>"rightarrow","onClick"=>"MoveOption(customization_form.sEnabled,customization_form.sDisabled);");

$filter_elements[0]['Selection']= $form->generateInputTag()."<br>";

$form->properties=array("type"=>"button","name"=>"Enable","title"=>"click me to deselect","class"=>"leftarrow","accesskey"=>"3","onClick"=>"MoveOption(customization_form.sDisabled, customization_form.sEnabled);");

$filter_elements[0]['Selection'].=$form->generateInputTag()."<br>";

$form->properties=array("type"=>"button","class"=>"sort","name"=>"selEnable",
"class"=>"sort", "title"=>"Sort Available
Filters","accesskey"=>"3","onClick"=>"sortSelect(customization_form.sEnabled);")
;

$filter_elements[0]['Selection'].=$form->generateInputTag();

$_POST["sDisabled"]=$_POST["sDisabled"]?$_POST["sDisabled"]:$customized_filter[0]["filter_id"];
$filter_elements[0]['Selected Filters']=
	$form->generateSelectMenu("sDisabled",$_POST["sDisabled"],$customized_filter,"","10","","onChange=\"filterElements(this.options[this.selectedIndex].value,this.options[this.selectedIndex].text);\"ondblClick=\"MoveOption(customization_form.sDisabled,customization_form.sEnabled);\"title='Double Click to deselect'","xmedium");

$form->properties=array("type"=>"button","name"=>"mvup","title"=>"Click here to move up","class"=>"uparrow","accesskey"=>"3","onClick"=>"moveOptionUp(customization_form.sDisabled)");

$filter_elements[0]['Display Order']=$form->generateInputTag()."<br>";

$form->properties=array("type"=>"button","name"=>"mvdown","title"=>"Click here to move down","class"=>"downarrow","accesskey"=>"3","onClick"=>"moveOptionDown(customization_form.sDisabled);");

$filter_elements[0]['Display Order'].=$form->generateInputTag();
$form->properties=array("type"=>"button","name"=>"sortselected","title"=>"Sort Selected Filters","class"=>"sort","accesskey"=>"3","onClick"=>"sortSelect(customization_form.sDisabled);");

$filter_elements[0]['Display Order'].=$form->generateInputTag();
$filter_elements[0]['Default Value']=
	$form->generateSelectMenu("filter_default_value",$_POST["filter_default_value"],"","--Select--","10","yes","onchange=assignValues(this.form.filter_default_value,customization_form.sDisabled.options[sDisabled.selectedIndex].value);","xmedium");
$form->properties=array("type"=>"text","name"=>"customization_name","value"=>$_POST[customization_name],"size"=>"22","title"=>"Enter customization name","onblur"=>"if(this.value.toLowerCase()=='default'){alert('Default Name not allowed !');this.value='';}");

	$db->query="select is_active from $sys_report_customization_table where customization_id=$customization_id and is_active='t'";
	$db->runQuery();
	$activate_status_array=$db->returnArrays();
	if($activate_status_array)
		$activate_status="checked";
	else
		$activate_status="";
echo "<br><center>Customization Name&nbsp; ".$form->generateInputTag().$form->errors["customization_name"];
echo "&nbsp; Make Default <input type=\"checkbox\" name=\"activate\" $activate_status></center>";
$_POST["availablecolumns"]=$_POST["availablecolumns"]?$_POST["availablecolumns"]:$non_customized_columns_array[0]["column_id"];
//for selecting report columns
$form_elements[0]['Available Columns']=
	$form->generateSelectMenu("availablecolumns",$_POST["availablecolumns"],$non_customized_columns_array,"","10","","ondblClick=\"MoveOption(customization_form.availablecolumns,customization_form.selectedcolumns);\"title='Double Click to select'","xmedium");

$form->properties=array("type"=>"button","name"=>"select","title"=>"Click here to move right","class"=>"rightarrow","accesskey"=>"3","onClick"=>"MoveOption(customization_form.availablecolumns, customization_form.selectedcolumns);");

$form_elements[0]['Selection']= $form->generateInputTag()."<br>";

$form->properties=array("type"=>"button","name"=>"deselect","title"=>"Click here to move left","class"=>"leftarrow","accesskey"=>"3","onClick"=>"MoveOption(customization_form.selectedcolumns, customization_form.availablecolumns);");

$form_elements[0]['Selection'].=$form->generateInputTag()."<br>";

$form->properties=array("type"=>"button","name"=>"show","title"=>"Sort Available Columns","class"=>"sort","accesskey"=>"3","onClick"=>"sortSelect(customization_form.availablecolumns);","size"=>"4");

$form_elements[0]['Selection'].=$form->generateInputTag();

$_POST["selectedcolumns"]=$_POST["selectedcolumns"]?$_POST["selectedcolumns"]:$customized_columns_array[0]["column_id"];

$form_elements[0]['Selected Columns']=
	$form->generateSelectMenu("selectedcolumns",$_POST["selectedcolumns"],$customized_columns_array,"","10","","onChange=\"if(last_id!=''){assignColumnValues(last_id);}last_id=this.options[this.selectedIndex].value;updateRow(this.options[this.selectedIndex].value,this.options[this.selectedIndex].text);document.customization_form.display_name.focus();\"ondblClick=\"MoveOption(customization_form.selectedcolumns,customization_form.availablecolumns);\"title='Double Click to deselect'","xmedium");

$form->properties=array("type"=>"button","name"=>"moveup","title"=>"Click here to move up","class"=>"uparrow","accesskey"=>"3","onClick"=>"moveOptionUp(customization_form.selectedcolumns)");

$form_elements[0]['Display Order']=$form->generateInputTag()."<br>";

$form->properties=array("type"=>"button","name"=>"movedown","title"=>"Click here to move down","class"=>"downarrow","accesskey"=>"3","onClick"=>"moveOptionDown(customization_form.selectedcolumns);");
$form_elements[0]['Display Order'].=$form->generateInputTag();

$form->properties=array("type"=>"button","name"=>"selEnable1","class"=>"sort","title"=>"Sort Selected Columns","accesskey"=>"3","onClick"=>"sortSelect(customization_form.selectedcolumns);");

$form_elements[0]['Display Order'].=$form->generateInputTag();

	$alignment_array["Left"]="text-align:left";
	$alignment_array["Center"]="text-align:center";
	$alignment_array["Right"]="text-align:right";
	formatConstraintArray($alignment_array);

	$form->properties=array("type"=>"text","name"=>"column_name","value"=>"","disabled"=>"disabled");
	$column_mapping_details[0]["Column Name"]=$form->generateInputTag();

	$form->properties=array("type"=>"text","name"=>"display_name","title"=>"Enter display name","value"=>"","onBlur"=>"if(this.value==\"\"){this.value=customization_form.column_name.value;}");
	$column_mapping_details[0]["Display Name"]=$form->generateInputTag();

	$column_mapping_details[0]["Alignment"]=$form->generateSelectMenu("style","",$alignment_array,"","","yes","narrow");
//d't delete
 	$form->properties=array("type"=>"hidden","name"=>"format_details","title"=>"Enter format details","value"=>"","size"=>"8");
 	echo $form->generateInputTag();

	$form->properties=array("type"=>"text","name"=>"decimal_places","title"=>"Enter decimal places","value"=>"","size"=>"8");
	$column_mapping_details[0]["Decimal Places"]=$form->generateInputTag();

	$column_mapping_details[0]["Group Column"]="<input type=\"checkbox\" name=\"group_column\" value=\"t\"   >";

	$column_mapping_details[0]["Need Total"]="<input type=\"checkbox\" name=\"display_summary\" value=\"t\" >";

	
	echo "<div align=\"right\" style=\"position:relative;width:80%;\"><a ID=\"report\"  href=\"javascript:Toggle('report_custom','report');resetItem('filter_custom','filter')\" style=\"background-color:#CDCDC1;border:1px solid black;\"><font color=\"black\">
	&nbsp;Columns Selection&nbsp;</font></a> ";
	echo "<a ID=\"filter\" href=\"javascript:Toggle('filter_custom','filter');resetItem('report_custom','report')\" style=\"background-color:#EEE8CD;border:1px solid black;\"><font color=\"black\">
	 &nbsp;Filters Selection&nbsp;</font></a>&nbsp;&nbsp;</div>";

	echo "<div style=\"background-color:#CDCDC1;border:1px solid black;width:80%;height:340px;\">";
	echo "<div id=\"report_custom\" style=\"display:block;\">";
	echo "<div align=\"center\"><h3>Report Columns Selection</h3><br>";
 
doHorizontalTable($form_elements,'20%',"","",true,false,"","general_table",""
) ;
	doHorizontalTable($column_mapping_details,"40%","","",true,false,"",
"general_table","");
	$form->properties=array("type"=>"button","name"=>"exit","value"=>"Exit",
"onclick"=>
"javascript:window.open
('../../lrp/reports/report_page.php?report_id=$report_id','_self');");
	echo "<br>".$form->generateInputTag();
	$form->properties=array("type"=>"button","name"=>"continue","value"=>"Continue","onclick"=>"resetItem('report_custom','report');Toggle('filter_custom','filter');");
	echo " ".$form->generateInputTag();
	$form->properties=array("type"=>"submit","name"=>"save","value"=>"Save");
	echo "&nbsp;".$form->generateInputTag();
	echo "</div></div>";
	echo "<div id=\"filter_custom\" style=\"display:none;\">";
	echo "<div align=\"center\"><h3>Report Filters Selection</h3><br></div>";
	doHorizontalTable($filter_elements,'20%',"","",true,false,"",
"general_table","");
	$form->properties=array("type"=>"button","name"=>"exit","value"=>"Exit",
"onclick"=>"javascript:window.open
('../../documents/reports/report_page?report_id= $report_id','_self');");
	echo "<br><div align =\"center\">";
	echo $form->generateInputTag();
	
	$form->properties=array("type"=>"button","name"=>"back","value"=>"Back","onclick"=>"Toggle('report_custom','report');resetItem('filter_custom','filter');");
	echo " ".$form->generateInputTag();
	$form->properties=array("type"=>"submit","name"=>"save","value"=>"Save");
	echo "&nbsp;".$form->generateInputTag();
	echo "</div></div><br>";


?>
    <input name="selected_filters" type="hidden" value="">
    <input name="selected_columns" type="hidden" value="">
    <input name="customization_id" type="hidden" value="<?php echo $customization_id ?>" >
    <br>
    <?
    
    echo "</form>";
//$page->doFooterHtml();
?>



<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

function Toggle(item,a_id)
	{
	obj=document.getElementById(item);
	document.getElementById(a_id).style.backgroundColor='#d6ebff';
	visible=(obj.style.display!="none");
	obj.style.display="block";
	}
function resetItem(item,a_id)
	{
	obj=document.getElementById(item);
	document.getElementById(a_id).style.backgroundColor='#BFBFBF';
	obj.style.display="none";
   	}

function MoveOption(objSourceElement, objTargetElement)
	{
	var values= new Array();
	var aryTempSourceOptions = new Array();
	var x = 0;
	var selected_item=-1;
	//looping through source element to find selected options
	for (var i = 0,flag=0; i < objSourceElement.length; i++)
		{
		    if (objSourceElement.options[i].selected)
	    		{
	    //file:///usr/share/doc/HTML/index.html
		//need to move this option to target element
			var intTargetLen = objTargetElement.length++;
			objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
			objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
			selected_item=i+1;
			flag++;
			
	    		}
	    	else
	    		{
			//storing options that stay to recreate select element
			var objTempValues = new Object();
			objTempValues.text = objSourceElement.options[i].text;
			objTempValues.value = objSourceElement.options[i].value;
			aryTempSourceOptions[x] = objTempValues;
			x++;
	   		}

		}
	

	//resetting length of source
	objSourceElement.length = aryTempSourceOptions.length;
	//looping through temp array to recreate source select element

	for (var i = 0; i < aryTempSourceOptions.length; i++)
		{
		objSourceElement.options[i].text = aryTempSourceOptions[i].text;
		objSourceElement.options[i].value = aryTempSourceOptions[i].value;
		objSourceElement.options[i].selected = false;
		}
	if(selected_item!=-1)
		{
		if(selected_item>flag)
			selected_item=selected_item-flag;
		else
			selected_item=flag-selected_item;
		objSourceElement.options[selected_item].selected=true;
		}
   	}

       	function posted_values(objTargetElement)
	{
		var selected_filters;
		var values=new Array();
		for (var i = 0; i < objTargetElement.length; i++)
			{
			values[i]= objTargetElement.options[i].value;
			}
		   selected_filters=values.toString();
		   document.customization_form.selected_filters.value=selected_filters;

	}
	function posted_values1(objTargetElement)
	{
		var selected_columns;
		var values=new Array();
		for (var i = 0; i < objTargetElement.length; i++)
			{
			values[i]= objTargetElement.options[i].value;
			}
		   selected_columns=values.toString();
		   document.customization_form.selected_columns.value=selected_columns;

	}

	function sortSelect(objTargetElement)
	{
	var o = new Array();
	for (var i=0; i<objTargetElement.options.length; i++)
		{
		o[o.length] = new Option( objTargetElement.options[i].text,objTargetElement.options[i].value, objTargetElement.options[i].defaultSelected, objTargetElement.options[i].selected) ;
		}
	if (o.length==0)
	 	{
	  	return;
	  	}
	o = o.sort(
		function(a,b)
			{
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			}
		);

	for (var i=0; i<o.length; i++)
		{
		objTargetElement.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

	function hasOptions(obj)
	{
	if (obj!=null && obj.options!=null)
		 {
		 return true;
		 }
	return false;
	}

	function swapOptions(obj,i,j)
	{
		var o = obj.options;
		var i_selected = o[i].selected;
		var j_selected = o[j].selected;
		var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
		o[i] = temp2;
		o[j] = temp;
		o[i].selected = j_selected;
		o[j].selected = i_selected;
	}

	function moveOptionUp(obj)
	{
	if (!hasOptions(obj))
		{
		return;
		}
		for (i=0; i<obj.options.length; i++)
		{
			if (obj.options[i].selected)
			{
				if (i != 0 && !obj.options[i-1].selected)
				{
					swapOptions(obj,i,i-1);
					obj.options[i-1].selected = true;
				}
			}
		}
	}
	function moveOptionDown(obj)
	{
	if (!hasOptions(obj))
		{
		return;
		}
		for (i=obj.options.length-1; i>=0; i--)
			{
			if (obj.options[i].selected)
				{
					if (i != (obj.options.length-1) && ! obj.options[i+1].selected)
					{
						swapOptions(obj,i,i+1);
						obj.options[i+1].selected = true;
					}
				}
			}
	}

</script>
<script type="text/Javascript">
<?php
foreach($master_filter_details_array as $key1=>$value1)
	{
	echo 'var _'.$value1["filter_id"].'="notset";';
	}
if($customized_filter_details_array)
	{
	foreach($customized_filter_details_array as $key=>$value)
		{
		if($value["default_value"])
			{
			echo '_'.$value["filter_id"].'="'.$value["default_value"].'";';
			}
		}
	}

?>
</script>
<script type="text/Javascript">
var last_id="";
function selectObjectSet()
        {
        this.value=new Array();
        this.text=new Array();
        }
<?php

foreach($filter_array as $key1=>$value1)
        {
        echo 'var filter_event_array'.$key1.'=new selectObjectSet();';
	if($value1)
	{
        foreach($value1 as $key2=>$value2)
                {

		foreach($value2 as $key3=>$value3)
	                {
			echo 'filter_event_array'.$key1.'.value['.$key2.']="'.$key2.'";';
			if($value3)
			echo 'filter_event_array'.$key1.'.text['.$key2.']="'.$value3.'";';
			else
			echo 'filter_event_array'.$key1.'.text['.$key2.']="--";';	
			}

                }
	}
        }

?>
function filterElements(index,form_display)
	{
	//filter article elements

	var default_index;
	var j=0;
	filter_temp_array=index.split('-');
	filter_id=filter_temp_array[0];
	filtername='filter_'+filter_temp_array[1];
	store=eval("filter_event_array"+filter_id);
        var c=store.text.length;
	document.customization_form.filter_default_value.options.length=store.text.length;

	hidden_value="";
	document.customization_form.filter_default_value.options[0] = new Option('--All--','0');
	for(i=1;i<=c;i++)
        	{
	        document.customization_form.filter_default_value.options[i] = new Option(store.text[i-1],store.value[i-1]);
		if(document.customization_form.eval(filtername).text===undefined)
			{
			db_default=eval("_"+filter_id);
			if(db_default!='notset')
				{
				db_default_array=db_default.split('}');
				if(store.text[i-1]==db_default_array[j])
					{
					document.customization_form.filter_default_value[eval(i)].selected=j+1;
					j++;
					hidden_value +=store.text[i-1]+'}';
					document.customization_form.eval(filtername).value=hidden_value;
					}
                		}
			else
				{
				document.customization_form.filter_default_value[0].selected=1;
				}
			}
		}

	if(document.customization_form.eval(filtername).text!==undefined)
		{
		var index1=document.customization_form.eval(filtername).text;
		default_index=index1.split('}');
		for(i=0;i<(default_index.length-1);i++)
			{
			document.customization_form.filter_default_value[eval(default_index[i])].selected=i+1;
			}
		}
	}
	//value-form (filter default value select menu object)
function assignValues(obj,index)
	{
	filtername="";
	filter_temp_array=index.split('-');
	filter_id=filter_temp_array[0];
	filtername='filter_'+filter_temp_array[1];
	var def_value="";
	document.customization_form.eval(filtername).text="";
	//document.customization_form.eval(filtername).value="";
	for(i=1;i<obj.length;i++)
		{
		if(obj.options[i].selected)
			{
			def_value +=obj.options[i].text+'}';
			document.customization_form.eval(filtername).text+=i+'}';
			}
		}
	//document.customization_form.eval(filtername).text=def_value;
	document.customization_form.eval(filtername).value=def_value;
	}
</script>
<script type="text/Javascript">
<?
echo 'var custom_column_id=new selectObjectSet();';
foreach($customized_column_details_array as $key1=>$value1)
        {
	echo 'var custom_report_event_array'.$value1["column_id"].'=new selectObjectSet();';
	echo 'custom_column_id.value['.$key1.']='.$value1["column_id"].';';
	// echo 'alert(custom_column_id.value['.$key1.']);';
	 foreach($value1 as $key2=>$value2)
                {
		echo 'custom_report_event_array'.$value1["column_id"].'.value["'.$key2.'"]="'.$value2.'";';

		}
	}

foreach($master_column_details_array as $key1=>$value1)
        {
	echo 'var master_report_event_array'.$value1["column_id"].'=new selectObjectSet();';
	 foreach($value1 as $key2=>$value2)
                {
		echo 'master_report_event_array'.$value1["column_id"].'.value["'.$key2.'"]="'.$value2.'";';

		}
	}

?>
function updateRow(column_id,display_name)
	{
	custom=0;
	var column_row_details="";
	store=eval("master_report_event_array"+column_id);
	document.customization_form.column_name.value=store.value["display_name"];
	for(i=0;i<custom_column_id.value.length;i++)
		{
		if(custom_column_id.value[i]==column_id)
			custom=1;
		}
	if(custom)
		{
		store=eval("custom_report_event_array"+column_id);
		custom=0;
		}
	master_store=eval("master_report_event_array"+column_id);
	column_row_details=document.customization_form.eval("column_"+master_store.value["script_variable_name"]).value;
	//alert(column_row_details);
	if(column_row_details!="")
		{
		entered_value=column_row_details.split('}');
		document.customization_form.display_name.value=entered_value[1];
		document.customization_form.style.value=entered_value[2];
		document.customization_form.format_details.value=entered_value[3];
		document.customization_form.decimal_places.value=entered_value[4];
		if(entered_value[5]=='t')
			document.customization_form.group_column.checked=true;
		else
			document.customization_form.group_column.checked=false;
		if(master_store.value["display_summary"]=='t')
			{
			document.customization_form.display_summary.disabled=false;
			if(entered_value[6]=='t')
				{
				document.customization_form.display_summary.checked=true;
				}
			else
				document.customization_form.display_summary.checked=false;

			}
		else
			{
			document.customization_form.display_summary.disabled=true;
			document.customization_form.display_summary.checked=false;
			}
		}
	else
		{
		
		document.customization_form.display_name.value=store.value["display_name"];
		document.customization_form.style.value=store.value["style"];
		document.customization_form.format_details.value=store.value["format_details"];
		document.customization_form.decimal_places.value=store.value["decimal_places"];
		if(store.value["display_summary"]=='t')
			{
			document.customization_form.display_summary.disabled=false;
			document.customization_form.display_summary.checked=true;
			}
		else
			document.customization_form.display_summary.checked=false;

		document.customization_form.group_column.checked=false;

		}
	}
function assignColumnValues(column_id)
	{

	if(column_id)
	{
	column_details="";
	master_store=eval("master_report_event_array"+column_id);
	column_details=column_id+'}';
	column_details+=document.customization_form.display_name.value+'}';
	column_details+=document.customization_form.style.value+'}';
	column_details+=document.customization_form.format_details.value+'}';
	column_details+=document.customization_form.decimal_places.value+'}';
	if(document.customization_form.group_column.checked==true)
		{
		column_details+=document.customization_form.group_column.value+'}';
		}
	else
		column_details+='}';
	if(document.customization_form.display_summary.checked==true)
		column_details+=document.customization_form.display_summary.value+'}';
	else
		column_details+='}';

	document.customization_form.eval("column_"+master_store.value["script_variable_name"]).value=column_details;
	}
}

</script>
